package cn.project.lemontree.controller;

import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.*;
import cn.project.lemontree.bean.Class;
import cn.project.lemontree.service.*;
import cn.project.lemontree.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/task")
@Api(tags = "任务发布管理模块")
@CrossOrigin  //跨域访问
public class TaskController {
    @Resource
    private TaskService taskService;
    @Resource
    private ClassService classService;
    @Resource
    private ChildService childService;
    @Resource
    private ParentService parentService;
    @Resource
    private PcTaskService pcTaskService;

    @SystemControllerLog(description = "分页查询任务信息")
    @ApiOperation("分页查询任务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="teacherName",value="老师姓名",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name="theme",value="主题",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult<Map<String, Object>> listpage(int page, int size,
                                                      String teacherName,String theme){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",taskService.listpage(page,size,teacherName,theme));
        map.put("count",taskService.count(teacherName,theme));
        map.put("page",page);
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "删除任务")
    @ApiOperation("删除发布任务的记录")
    @PostMapping ("/remove")
    public CommonResult remove(@RequestParam("taskId") int taskId){
        return CommonResult.success(taskService.removeById(taskId));
    }

    @SystemControllerLog(description = "发布任务")
    @ApiOperation(value = "老师发布任务")
    @PostMapping("/save")
    public  CommonResult saveTask(@RequestBody Task task,Integer teacherId){
//        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        task.setTeacher(new Teacher(teacherId));
        taskService.saveTask(task);
        List<Task> tasks = taskService.regain(task.getDdesc(),task.getTheme());
        Class aclass = classService.findClassByid(teacherId);
        List<Child> children = childService.findChildrenByid(aclass.getClassId());
        for(Child child : children){
            List<Parent> parents = parentService.findParentByid(child.getChildId());
            for (Parent parent : parents){
                pcTaskService.savePCTask(new Parent_child_task(tasks.get(tasks.size() - 1),parent,child,0,0));
            }
        }
        return CommonResult.success(true);
    }

    @SystemControllerLog(description = "发布任务1")
    @ApiOperation(value = "老师发布任务1")
    @PostMapping("/add")
    public CommonResult addTask(@RequestBody Task task){
        Teacher teacher = new Teacher();
        teacher.setTeacherId(task.getTeacherId());
        task.setTeacher(teacher);
        return CommonResult.success(taskService.saveTask(task));
    }

    @SystemControllerLog(description = "修改任务1")
    @ApiOperation(value = "老师修改任务信息1")
    @PostMapping("/updatetask")
    public CommonResult update(@RequestBody Task task){
        Teacher teacher = new Teacher();
        teacher.setTeacherId(task.getTeacherId());
        task.setTeacher(teacher);
        return CommonResult.success(taskService.updateTask(task));
    }


    @SystemControllerLog(description = "修改任务")
    @ApiOperation(value = "老师修改任务信息")
    @PostMapping("/update")
    public  CommonResult updateTask(@RequestBody Task task,Integer teacherId){
//        User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
        task.setTeacher(new Teacher(teacherId));
        return CommonResult.success(taskService.updateTask(task));
    }
    @ApiOperation(value = "获取用户发布任务信息")
    @GetMapping("/getMyTask")
    public CommonResult getMyTask(int page,int size,String theme,Integer parentId){
//        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Map<String ,Object> map = new HashMap<>();
        map.put("data" ,taskService.getMyTask(page,size,parentId,theme));
        map.put("count",taskService.countMyTask(parentId,theme));
        map.put("page",page);
        return CommonResult.success(map);

    }
}
